<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class member extends core {
	private $member_mod;

	public function __construct() {
		parent::__construct();
		$this->member_mod = m("member");
	}

	//会员列表
	public function index() {
		//http://www.yanglanzi8.com/wap.php?app=member&act=detail&invite_code=xxxx
		//echo strtotime("2016-01-08")." ";
		//echo strtotime("2016-01-09");
		$where = '';
		$status = (isset($_GET['status'])) ? trim($_GET['status']) : '';
		$member_type = (isset($_GET['member_type'])) ? intval($_GET['member_type']) : 0;
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$member_id = (isset($_GET['member_id'])) ? $_GET['member_id'] : '';
		$shop_id = (isset($_GET['shop_id'])) ? $_GET['shop_id'] : '';
		$begin_date = (isset($_GET['begin_date'])) ? trim($_GET['begin_date']) : '';
		$end_date = (isset($_GET['end_date'])) ? trim($_GET['end_date']) : '';
		$invite_code = (isset($_GET['invite_code'])) ? trim($_GET['invite_code']) : '';
		$grade_id = (isset($_GET['grade_id'])) ? trim($_GET['grade_id']) : '';
		if (strlen($begin_date)) {
			$where .= " AND m.reg_time>='".strtotime($begin_date)."'";
		}
		if (strlen($end_date)) {
			$where .= " AND m.reg_time<='".strtotime($end_date)."'";
		}
		if ($keyword) {
			$where .= " AND (m.name LIKE '%{$keyword}%' OR m.mobile LIKE '%{$keyword}%' OR m.nick_name LIKE '%{$keyword}%')";
		}
		if (strlen($member_id)) {
			$where .= " AND m.id='{$member_id}'";
		}
		if (strlen($status)) {
			$where .= " AND m.status='{$status}'";
		}
		if ($member_type) {
			$where .= " AND m.member_type='{$member_type}'";
		}
		if ($invite_code) {
			$where .= " AND m.invite_code='{$invite_code}'";
		}
		if ($shop_id) {
			$where .= " AND m.shop_id='{$shop_id}'";
		}
		if ($grade_id) {
			$where .= " AND m.grade_id='{$grade_id}'";
		}
		//分页
		$this->setpages(array('member_id'=>$member_id, 'status'=>$status, 'member_type'=>$member_type,
			'keyword'=>$keyword, 'begin_date'=>$begin_date, 'end_date'=>$end_date,
			'invite_code'=>$invite_code, 'shop_id'=>$shop_id,'grade_id'=>$grade_id));

		$sql = "SELECT m.*, g.name as grade_name, '' as invitor FROM {$this->tbp}member m LEFT JOIN {$this->tbp}member_grade g ON m.grade_id = g.id
			WHERE 1 {$where} ORDER BY m.id DESC";
		//echo $sql;
		$rs = $this->ezr->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				$rs[$key]['status_name'] = $this->member_mod->status_name($row['status']);
				$rs[$key]['member_type_name'] = $this->member_mod->member_type($row['member_type']);
				if ($row['invite_code']) {
					$sql = "SELECT mobile, name FROM {$this->tbp}member WHERE mobile='".$row['invite_code']."'";
					$invitor = $this->db->get_row($sql);
					if ($invitor) {
						if ($invitor->name) {
							$rs[$key]['invitor'] = $invitor->name;
						} else {
							$rs[$key]['invitor'] = $invitor->mobile;
						}
					}
				}
				$url = "{$this->domain}/wap.php?reseller=".$row['id'];
				$url = urlencode($url);
				$rs[$key]['url'] = $url;
				$parent_id = SQL::share('member_invitation')->where("invitation_id='{$row['id']}'")->val('member_id');
                $rs[$key]['i_name'] = SQl::share('member')->where("id='{$parent_id}'")->val('name');
                $rs[$key]['i_nick_name'] = SQl::share('member')->where("id='{$parent_id}'")->val('nick_name');
                $rs[$key]['i_id'] = $parent_id;
			}
		}
		$nav = $this->ezr->get_navigation();
		//shop
		$sql = "SELECT id, name FROM {$this->tbp}shop ORDER BY id ASC";
		$shops = $this->db->get_results($sql);
		//grade
		$sql = "SELECT id, name FROM {$this->tbp}member_grade ORDER BY sort ASC";
		$grades = $this->db->get_results($sql);

		$this->smarty->assign('grades',$grades);
		$this->smarty->assign('shops',$shops);
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('status',$status);
		$this->smarty->assign('invite_code',$invite_code);
		$this->smarty->assign('member_id',$member_id);
		$this->smarty->assign('end_date',$end_date);
		$this->smarty->assign('begin_date',$begin_date);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('where',$where);
		$this->smarty->assign('shop_id',$shop_id);
		$this->smarty->assign('grade_id',$grade_id);
		$this->display();
	}

	//编辑
	public function edit() {
		if (IS_POST) {
			$this->save();
		} else {
			$id = (isset($_GET['id']) && intval($_GET['id'])) ? intval($_GET['id']) : 0;
			$sql = "SELECT * FROM {$this->tbp}member WHERE id='{$id}'";
			$row = $this->db->get_row($sql);
			$this->smarty->assign('row', $row);
			//店长
			$sql = "SELECT id, name, nick_name FROM {$this->tbp}member WHERE member_type=3";
			$shopowner = $this->db->get_results($sql);
			$this->smarty->assign('shopowner', $shopowner);
			//grade
			$sql = "SELECT id, name FROM {$this->tbp}member_grade ORDER BY sort ASC, id DESC";
			$grades = $this->db->get_results($sql);
			$this->smarty->assign('grades',$grades);
			//shops
			$sql = "SELECT id, name FROM {$this->tbp}shop ORDER BY sort ASC";
			$shops = $this->db->get_results($sql);
			$this->smarty->assign('shops',$shops);
			$this->display();
		}
	}

	//保存
	public function save() {
		$id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$status = (isset($_POST['status']) && intval($_POST['status'])) ? intval($_POST['status']) : 0;
		$nick_name = (isset($_POST['nick_name']) && trim($_POST['nick_name'])) ? trim($_POST['nick_name']) : '';
		$member_type = (isset($_POST['member_type']) && trim($_POST['member_type'])) ? intval($_POST['member_type']) : 0;
		$card_sn = (isset($_POST['card_sn']) && trim($_POST['card_sn'])) ? trim($_POST['card_sn']) : '';
		$mobile = (isset($_POST['mobile']) && trim($_POST['mobile'])) ? trim($_POST['mobile']) : '';
		$qq = (isset($_POST['qq']) && trim($_POST['qq'])) ? trim($_POST['qq']) : '';
		$invite_code = (isset($_POST['invite_code']) && trim($_POST['invite_code'])) ? trim($_POST['invite_code']) : '';
		$money = (isset($_POST['money']) && trim($_POST['money'])) ? trim($_POST['money']) : '';
		$commission = (isset($_POST['commission']) && trim($_POST['commission'])) ? trim($_POST['commission']) : '';
		$shopowner_id = (isset($_POST['shopowner_id']) && trim($_POST['shopowner_id'])) ? intval($_POST['shopowner_id']) : 0;
		$grade_id = (isset($_POST['grade_id']) && intval($_POST['grade_id'])) ? intval($_POST['grade_id']) : 0;
		$grade_score = isset($_POST['grade_score']) ? trim($_POST['grade_score']) : '';
		$belong_shop_id = (isset($_POST['belong_shop_id']) && trim($_POST['belong_shop_id'])) ? intval($_POST['belong_shop_id']) : 0;
		//$avatar = upload_one_file('avatar', 'avatar');
		if ($member_type !== 2) $shopowner_id = 0;
		if (strlen($grade_score)) {
			$grade_score = intval($grade_score);
			if ($grade_score<0) $grade_score = 0;
		}
		//编辑
		if ($id>0) {
			$sql = "UPDATE {$this->tbp}member SET nick_name='{$nick_name}', member_type='{$member_type}',
				commission='{$commission}', mobile='{$mobile}', qq='{$qq}', status='{$status}',
				invite_code='{$invite_code}', money='{$money}', shopowner_id='{$shopowner_id}',
				grade_id='{$grade_id}', grade_score='{$grade_score}', belong_shop_id='{$belong_shop_id}', card_sn='{$card_sn}'
				WHERE id='{$id}'";
			$this->db->query($sql);
			
			//通过等级积分检测是否需要等级升级处理
			if (strlen($grade_score)) $this->member_mod->update_grade_from_score($id);
		}
		header("Location:?app=member&act=index");
	}

	//删除
	public function delete() {
		$this->permission($this->admin_id, $this->app, $this->act);
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$sql = "DELETE FROM {$this->tbp}member WHERE id='{$id}'";
		$this->db->query($sql);
		header("Location:?app=member&act=index");
	}

	//导出
	public function export() {
		set_time_limit(0);
		$fields = array(
			'id'=>'ID',
			'real_name'=>'真实姓名',
			'mobile'=>'手机号码',
			'sex'=>'性别',
			'province'=>'省',
			'city'=>'市',
			'status_name'=>'状态',
			'logins'=>'登录次数',
			'reg_time'=>'注册时间',
		);
		$where = (isset($_GET['where']) && trim($_GET['where'])) ? trim($_GET['where']) : '';
		$where = stripcslashes($where);
		$sql = "SELECT m.*, '' as invitor  FROM {$this->tbp}member m
			WHERE 1 {$where} ORDER BY m.id DESC";
		//echo $sql;
		$rs = $this->db->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				$rs[$key]->status_name = $this->member_mod->status_name($row->status);
				// $rs[$key]->member_type_name = $this->member_mod->member_type($row->member_type);
				if ($row->reg_time) $rs[$key]->reg_time = date("Y-m-d H:i:s", $row->reg_time);
				if ($row->last_time) $rs[$key]->last_time = date("Y-m-d H:i:s", $row->last_time);
			}
			export_excel2($rs, $fields, "member");
		}
	}
	
	//减员申请
	public function person_reduce() {
		$where = '';
		$export = $this->request->get('export', 0);
		$id = $this->request->get('id');
		$name = $this->request->get('name');
		$status = $this->request->get('status');
		$begin_time = $this->request->get('begin_time');
		$end_time = $this->request->get('end_time');
		if (strlen($id)) {
			$where .= "AND pr.id='{$id}'";
		}
		if (strlen($status)) {
			$where .= "AND pr.status='{$status}'";
		}
		if (strlen($name)) {
			$persons = array();
			$rs = SQL::share('member_person')->where("name LIKE '%{$name}%'")->find('id');
			if ($rs) {
				foreach ($rs as $g) {
					$persons[] = "LOCATE(',{$g->id},', CONCAT(',',pr.persons,','))>0";
				}
				$where .= " AND (".implode(' OR ', $persons).")";
			}
		}
		if (strlen($begin_time)) {
			$where .= "AND pr.add_time>='".strtotime($begin_time)."'";
		}
		if (strlen($end_time)) {
			$where .= "AND pr.add_time<='".strtotime($end_time)."'";
		}
		if ($export>0) {
			$rs = SQL::share('person_reduce pr')->where($where)->sort('pr.id DESC')->find();
			$persons = array();
			if ($rs) {
				foreach ($rs as $k=>$g) {
					$per = SQL::share('collect c')
						->left('city cc', 'c.city_id=cc.city_id')
						->where("c.id IN ({$g->persons})")->find("cc.name as city_name, c.real_name, c.idcard, '{$g->status}' as status, '{$g->id}' as parent_id");
					if ($per) {
						foreach ($per as $p) {
							$persons[] = $p;
						}
					}
				}
			}
			export_excel($persons, array(
				'city_name'=>'城市',
				'real_name'=>'姓名',
				'idcard'=>'身份证',
				'status'=>'状态(1:同意，-1:不同意，0:未审核)',
				'parent_id'=>'申请表id(请不要修改)'
			));
		}
		$rs = SQL::share('person_reduce pr')->left('member m', 'm.id=pr.member_id')->where($where)->isezr()->sort('pr.id DESC')
			->find("pr.*, m.real_name as member_name");
		//echo SQL::share()->sql;exit;
		$page = SQL::share()->page;
		if ($rs) {
			foreach ($rs as $k=>$g) {
				$collects = SQL::share('collect c')
					->left('city cc', 'c.city_id=cc.city_id')
					->where("c.id IN ({$g->persons})")->find('cc.name as city_name, c.real_name');
				$persons = array();
				if ($collects) {
					foreach ($collects as $c) {
						$persons[] = $c->real_name." ({$c->city_name})";
					}
				}
				$rs[$k]->persons = implode('<br />', $persons);
			}
		}
		$this->smarty->assign('rs', $rs);
		$this->smarty->assign('page', $page);
		$this->smarty->assign('id', $id);
		$this->smarty->assign('name', $name);
		$this->smarty->assign('status', $status);
		$this->smarty->assign('begin_time', $begin_time);
		$this->smarty->assign('end_time', $end_time);
		$this->display();
	}
	public function person_reduce_edit() {
		$id = $this->request->get('id', 0);
		if (IS_POST) {
			$id = $this->request->post('id', 0);
			$status = $this->request->post('status', 0);
			if ($id<=0) error('缺少数据');
			SQL::share('person_reduce')->where($id)->update(array('status'=>$status));
			$row = SQL::share('person_reduce')->where($id)->row('persons');
			// if ($status==1) SQL::share('collect')->where("id IN ({$row->persons})")->update(array('status'=>3));
			location('?app=member&act=person_reduce');
		} else {
			$row = SQL::share('person_reduce pr')->left('member m', 'm.id=pr.member_id')->where($id)->find("pr.*, m.real_name as member_name");
			if ($row) {
				$collects = SQL::share('collect c')
					->left('city cc', 'c.city_id=cc.city_id')
					->where("c.id IN ({$row->persons})")->find('cc.name as city_name, c.real_name');
				$persons = array();
				if ($collects) {
					foreach ($collects as $c) {
						$persons[] = $c->real_name . " ({$c->city_name})";
					}
				}
				$row->persons = implode('<br />', $persons);
			}
		}
		$this->smarty->assign('row', $row);
		$this->display();
	}
	public function person_reduce_delete() {
		$id = $this->request->get('id', 0);
		SQL::share('person_reduce')->delete($id);
		location("?app=member&act=person_reduce");
	}
	
	public function person_reduce_check_cate() {
		$cate_type = $this->request->post('cate_type');
		if ($cate_type=='import') {
			$file = upload_one_file('person_reduce', 'file', false, false, false);
			if (!strlen($file)) error('文件错误，请重新上传');
			$datas = import_excel($file, 0, 2);
			//echo json_encode($datas);exit;
			foreach ($datas as $k => $g) {
				$city = trim($g['A']);
				$city_id = '';
				//if (strlen($city)) $city_id = SQL::share('city')->where("name LIKE '%" . trim($g['A']) . "%'")->val('city_id');
				$idcard = trim($g['B']);
				$name = trim($g['C']);
				$status = intval($g['D']);
				$parent_id = intval($g['E']);
				if ($parent_id>0) {
					SQL::share('person_reduce')->where($parent_id)->update(array('status'=>$status));
					$row = SQL::share('person_reduce')->where($parent_id)->row('persons');
					if ($status==1) SQL::share('collect')->where("id IN ({$row->persons})")->update(array('status'=>3));
				}
			}
		}
		location("?app=member&act=person_reduce");
	}
	
	//参保人列表
	public function person() {
		$where = '';
		$id = $this->request->get('id', 0);
		$status = $this->request->get('status');
		if ($id<=0) error('缺少参数');
		if (strlen($status)) {
			$where .= " AND status='{$status}'";
		}
		$rs = SQL::share('member_person')->where("member_id='{$id}' {$where}")->setpages(compact('id', 'status'))->isezr()->find();
		$this->smarty->assign('rs', $rs);
		$this->smarty->assign('page', SQL::share()->page);
		$this->display();
	}
	
	//下载身份证图片
	public function person_idcard() {
		$id = $this->request->get('id', 0);
		if ($id<=0) error('缺少参数');
		$row = SQL::share('member_person')->where($id)->row('name, idcard, idcard_pic1, idcard_pic2');
		if ($row) {
			$dir = UPLOAD_PATH."/idcard/".date('Y').'/'.date('m').'/'.date('d').'/'.$row->idcard;
			makedir($dir);
			download_file($dir, array($row->idcard_pic1, $row->idcard_pic2));
			export_zip($dir, $row->idcard, $row->name);
		}
	}

	//邀请人排行
	public function invitor_ranks() {
		$invitors = array();
		$mobiles = "";
		$where = (isset($_GET['where']) && trim($_GET['where'])) ? trim($_GET['where']) : '';
		$where = stripcslashes($where);
		$sql = "SELECT m.invite_code  FROM {$this->tbp}member m
			WHERE m.invite_code<>'' {$where} ORDER BY m.id DESC";
		//echo $sql;
		$rs = $this->db->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				if (array_key_exists($row->invite_code, $invitors)) {
					$invitors[$row->invite_code] += 1;
				} else {
					$invitors[$row->invite_code] = 1;
				}
			}
			arsort($invitors);
			$invitors = array_slice($invitors, 0, 20, true);
			foreach ($invitors as $mobile => $num) {
				if ($mobiles) {
					$mobiles .= ",{$mobile}";
				} else {
					$mobiles = "{$mobile}";
				}
			}

			//print_r($mobiles);
			print_r($invitors);
		}
	}

	public function choose(){
        $where = '';
		$status = (isset($_GET['status'])) ? trim($_GET['status']) : '';
		$member_type = (isset($_GET['member_type'])) ? intval($_GET['member_type']) : 0;
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$member_id = (isset($_GET['member_id'])) ? $_GET['member_id'] : '';
		$is_kefu = (isset($_GET['is_kefu'])) ? $_GET['is_kefu'] : '';
		$begin_date = (isset($_GET['begin_date'])) ? trim($_GET['begin_date']) : '';
		$end_date = (isset($_GET['end_date'])) ? trim($_GET['end_date']) : '';
		$invite_code = (isset($_GET['invite_code'])) ? trim($_GET['invite_code']) : '';
		if (strlen($begin_date)) {
			$where .= " AND m.reg_time>='".strtotime($begin_date)."'";
		}
		if (strlen($end_date)) {
			$where .= " AND m.reg_time<='".strtotime($end_date)."'";
		}
		if ($keyword) {
			$where .= "AND (m.name LIKE '%{$keyword}%' OR m.mobile LIKE '%{$keyword}%')";
		}
		if (strlen($member_id)) {
			$where .= "AND m.id='{$member_id}'";
		}
		if (strlen($status)) {
			$where .= "AND m.status='{$status}'";
		}
		if ($member_type) {
			$where .= "AND m.member_type='{$member_type}'";
		}
		if ($invite_code) {
			$where .= "AND m.invite_code='{$invite_code}'";
		}
		//分页
		$this->setpages(array('member_id'=>$member_id, 'status'=>$status, 'member_type'=>$member_type,
			'keyword'=>$keyword, 'begin_date'=>$begin_date, 'end_date'=>$end_date, 'invite_code'=>$invite_code));

		$sql = "SELECT m.*, '' as invitor  FROM {$this->tbp}member m
			WHERE 1 {$where} ORDER BY m.id DESC";
		//echo $sql;
		$rs = $this->ezr->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				$rs[$key]['status_name'] = $this->member_mod->status_name($row['status']);
				$rs[$key]['member_type_name'] = $this->member_mod->member_type($row['member_type']);
				if ($row['invite_code']) {
					$sql = "SELECT mobile, name FROM {$this->tbp}member WHERE mobile='".$row['invite_code']."'";
					$invitor = $this->db->get_row($sql);
					if ($invitor) {
						if ($invitor->name) {
							$rs[$key]['invitor'] = $invitor->name;
						} else {
							$rs[$key]['invitor'] = $invitor->mobile;
						}
					}
				}
			}
		}
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('status',$status);
		$this->smarty->assign('is_kefu',$is_kefu);
		$this->smarty->assign('invite_code',$invite_code);
		$this->smarty->assign('member_id',$member_id);
		$this->smarty->assign('end_date',$end_date);
		$this->smarty->assign('begin_date',$begin_date);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('where',$where);
		$this->display();
    }

}

